<style scoped lang="stylus">
@import '../../assets/commonCss/base.styl';
@import 'login.styl';
</style>
<template>
  <div id="block">
    <div class="login">
      <div class="title">
        <h1>{{_SERVEROBJ.title}}V1.0</h1>
        <p>政府web版</p>
      </div>
      <form action="" @keyup.enter="login()">
        <label for="name">
          <i class="iconfont icon-zhanghu"></i>
          <input type="text" id="name" v-model="username" placeholder="帐号">
        </label>
        <label for="pw">
          <i class="iconfont icon-mima-copy"></i>
          <input type="password" v-model="password" id="pw" placeholder="密码">
        </label>
        <div @click="login()" class="sub">登录</div>
      </form>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import $ from "jquery";
import sha1 from "js-sha1";

export default {
  name: "Login",
  data() {
    return {
      username: "",
      password: ""
    };
  },
  created() {
    if (
      navigator.appName == "Microsoft Internet Explorer" &&
      parseInt(
        navigator.appVersion
          .split(";")[1]
          .replace(/[ ]/g, "")
          .replace("MSIE", "")
      ) < 10
    ) {
      this.$router.push({ path: "reminder" });
    }
  },
  computed: {},
  methods: {
    login() {
      let errorMsg = !this.username
        ? "请输入账号" : "";
      if (errorMsg) {
        this.$Notice.error({
          title: "错误",
          desc: errorMsg
        });
        return;
      }
      const regPsd = /(?=.*[A-Z])(?=.*[a-z])(?!^\d+$)(?!^[a-zA-z]+$)[0-9a-zA-z]{8,23}/;
      if (this.password == "") {
        this.$Notice.error({
          title:'错误',
          desc: '请输入密码'
        })
        return;
      }
      if (this.password.length < 8) {
        this.$Notice.error({
          title:'错误',
          desc: '密码至少大于等于8位'
        })
        return;
      }
      if (this.password.length > 24) {
        this.$Notice.error({
          title:'错误',
          desc: '密码不能大于24位'
        })
        return;
      }
      if (!regPsd.test(this.password)) {
        this.$Notice.error({
          title:'错误',
          desc: '密码只能由数字和字母和大写字母组成'
        })
        return;
      }
      let sha = sha1(this.password).toLowerCase();
      $.ajax({
        url: this._URL_v2 + "/user/login",
        type: "post",
        xhrFields: {
          withCredentials: true
        },
        data: {
          userId: this.username,
          // password: sha
           pwd: sha
        },
        dataType: "json",
        complete: xhr => {
          if (xhr.status == 200) {
            const res = xhr.responseJSON;

            if (res.code == 0) {
              let usermsg = res.result;
              sessionStorage.clear();
              sessionStorage.setItem("ZHJF", sha);
              sessionStorage.setItem("userMsg", JSON.stringify(usermsg));
              this.$store.commit("isLogin", usermsg);
              this.$Notice.success({
                title: "",
                desc: "登录成功!",
                duration: 2
              });
              this.$router.push({ path: "/" });
            } else {
              this.$Notice.error({
                title: "错误",
                desc: "错误信息: " + res.message
              });
            }
          }
        }
      });
    }
  }
};
</script>